package com.qsmx.core.webfile;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebSettings;

/**
 * @ClassName: WebViewInitializer
 * @Description: java类作用描述
 * @Author: Jove.Nan
 * @Date: 2019/3/22 5:47 PM
 */
public class WebViewInitializer {

    @SuppressLint("SetJavaScriptEnabled")
    public FileWebView createWebView(FileWebView webView, Context context) {

        webView.setWebContentsDebuggingEnabled(true);
        //cookie相关
        final CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            cookieManager.setAcceptThirdPartyCookies(webView, true);
        }
        CookieManager.setAcceptFileSchemeCookies(true);
        //不能横向滑动
        webView.setHorizontalScrollBarEnabled(false);
        //不能纵向滑动
        webView.setVerticalFadingEdgeEnabled(false);
        //允许截图
        webView.setDrawingCacheEnabled(true);
        //屏蔽长按
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                return true;
            }
        });
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        //初始化WebSetting
        final WebSettings settings = webView.getSettings();
        final String ua = settings.getUserAgentString();
        settings.setDisplayZoomControls(false);
        //文件权限
        settings.setAllowFileAccess(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setAllowContentAccess(true);
        //缓存相关
        settings.setDatabaseEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
        settings.setJavaScriptEnabled(true);//是否允许JavaScript脚本运行，默认为false。设置true时，会提醒可能造成XSS漏洞
        settings.setSupportZoom(true);//是否可以缩放，默认true
        settings.setBuiltInZoomControls(true);//是否显示缩放按钮，默认false
        settings.setUseWideViewPort(true);//设置此属性，可任意比例缩放。大视图模式
        settings.setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        settings.setDomStorageEnabled(true);//开启本地DOM存储
        settings.setLoadsImagesAutomatically(true); // 加载图片
        settings.setMediaPlaybackRequiresUserGesture(false);//播放音频，多媒体需要用户手动？设置为false为可自动播放
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);

        return webView;
    }
}
